const User = require('../model/User.js');
module.exports = {
	register: function(req, res){
		User.findOne({phone: req.body.phone}, (err, data)=>{
			if(data == null){
				var user = new User({
					phone: req.body.phone,
					password: req.body.password,
					head_img: "",
					name: "",
					birthday: "",
					aim: "",
					wechat: "",
					QQ: ""
				});
				user.save((err, data)=>{
					res.send("register");
				});
			} else{
				res.send('用户已存在');
			}
		})
	},
	login: function(req, res){
		User.findOne({phone: req.body.phone}, (err, data)=>{
			if(data == null){
				res.send({
					status: "用户不存在"
				});
			} else{
				if(data.password == req.body.password){
					res.send({
						status: "登录成功",
						user_id: data._id
					})
				} else{
					res.send({
						status: "密码错误"
					});
				}
			}
		})
	},
	updatePwd: function(req, res){
		User.updateOne({phone: req.body.phone}, {$set: {password: req.body.password}},(err, data)=>{
			res.send('修改密码成功');
		})
	},
	updateInfo: function(req, res){
		User.updateOne({phone: req.body.phone}, {$set: req.body},(err, data)=>{
			res.send('修改个人信息成功');
		})
	}
}